< previous page page_813 next page >

Page 813
0813-01.jpg
Figure C.8
The table shows the summary
information from aggregate functions.
Listing C.14 SUMMARY.TXTUsing Aggregate Functions to Provide Summary Information
SELECT Min(SL.Quantity * RT.Retail) AS Minsls,  _
     Max(SL.Quantity * RT.Retail) AS Maxsls,  _
     Avg(SL.Quantity * RT.Retail) AS Avgsls,  _
     Sum(SL.Quantity * RT.Retail) AS Totsls,  _
     Sum(SL.Quantity) AS Totvol  _
     FROM Sales AS SL, [Retail Items] AS RT  _
     WHERE SL.[Item Code]=RT.[Item Code]
Creating Record Groups
Creating record groups lets you create a recordset that has only one record for each occurrence of the specified field. For example, if your group the Customers table by state, you have one output record for each state. This arrangement is especially useful when combined with the calculation functions described in the preceding sections. When groups are used in conjunction with aggregate functions, you can easily obtain summary data by state, salesperson, item code, or any other desired field.
Most of the time, you want to create groups based on a single field. You can, however, specify multiple fields in the GROUP BY clause. If you do, a record is returned for each unique combination of field values. You can use this technique to get sales data by salesperson and item code. Separate multiple fields in a GROUP BY clause with commas. Listing C.15 shows an update of Listing C.14, adding groups based on the salesperson ID. Figure C.9 shows the results of the query.
Listing C.15 GROUP.TXTUsing the GROUP BY Clause to Obtain Summary Information for Record Groups
SELECT SL.SalesID, Min(SL.Quantity * RT.Retail) AS Minsls,  _
     Max(SL.Quantity * RT.Retail) AS Maxsls,  _
     Avg(SL.Quantity * RT.Retail) AS Avgsls,  _
     Sum(SL.Quantity * RT.Retail) AS Totsls,  _
     Sum(SL.Quantity) AS Totvol  _
     FROM Sales AS SL, [Retail Items] AS RT  _
     WHERE SL.[Item Code]=RT.[Item Code]  _
     GROUP BY SL.SalesID

 
< previous page page_813 next page >